package com.example.pandaso.controller;


import com.example.pandaso.pojo.TopWeb;
import com.example.pandaso.service.TopWebService;
import com.example.pandaso.utils.ResponseUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class TopWebController {

    @Autowired
    private TopWebService topWebService;
    @GetMapping("/getTop")
    public ResponseEntity<List<TopWeb>> listAll(){
        return ResponseEntity.ok(topWebService.listAll());
    }
    @PostMapping("/addTop")
    public ResponseEntity<?> add(@RequestBody TopWeb topWeb){
        String res = Boolean.toString(topWebService.saveOne(topWeb));
        if("true".equals(res)) {
            return ResponseEntity.ok(res);
        }else {
            return ResponseUtils.error(HttpStatus.CONFLICT,"duplicate url");
        }
    }


    @PostMapping("/starClick")
    public ResponseEntity<?> starClick(@RequestBody Integer topWebId) {
        System.out.println(topWebId);
        boolean result = topWebService.starClick(topWebId);
        if (result) {
            return ResponseEntity.ok("success");
        } else {
            return ResponseUtils.error(HttpStatus.CONFLICT, "duplicate url");
        }
    }


}
